package net.tarine.gastronomi.client.forms;

import net.tarine.gastronomi.client.AuthSingleton;
import net.tarine.gastronomi.client.IAuthenticatedWidget;
import net.tarine.gastronomi.shared.to.ForumMessageTo;
import net.tarine.gastronomi.shared.to.ForumThreadTo;
import net.tarine.gastronomi.shared.to.GasUserTo;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MessageEditPanel extends SimplePanel implements IAuthenticatedWidget {

	private ForumThreadTo thread = null;
	private ForumMessageTo item = null;
	private GasUserTo user = null;
	
	private VerticalPanel container = null;
	private TextArea textArea = null;

	public MessageEditPanel(ForumThreadTo thread, ForumMessageTo message) {
		this.thread = thread;
		if (message != null) {
			this.item = message;
		} else {
			this.item = new ForumMessageTo();
		}
		AuthSingleton.get().authorizeWidget(this);
	}
	
	@Override
	public void unlockWidget(GasUserTo user) {
		this.user = user;
		drawItem();
	}
	
	private void drawItem() {
		this.clear();
		container = new VerticalPanel();
		if (thread.getId() != null) {
			HTML msgLabel = new HTML("Rispondi:");
			msgLabel.setStyleName("message-label");
			container.add(msgLabel);
		} else {
			HTML msgLabel = new HTML("Scrivi il messaggio:");
			msgLabel.setStyleName("message-label");
			container.add(msgLabel);
		}
		textArea = new TextArea();
		textArea.setStyleName("message-text-area");
		textArea.setValue(item.getText());
		container.add(textArea);
		this.add(container);
	}
	
	public ForumMessageTo getForumMessage() {
		item.setText(textArea.getValue());
		item.setGasUserTo(user);
		return item;
	}


}
